package com.rfid.base.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public abstract class BaseAdapter<VH extends RecyclerView.ViewHolder,T> extends RecyclerView.Adapter<VH> {

    protected Listener mListener;
    protected List<T> mData;
    protected boolean disableClick;

    public BaseAdapter(List<T> data) {
        mData = data;
    }

    public void setListener(Listener listener) {
        mListener = listener;
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        if (!disableClick) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        mListener.onClick(mData.get(position));
                    }
                }
            });
        }
        bindView(holder,position,mData.get(position));
    }

    public T getItem(int pos) {
        return mData.get(pos);
    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return getViewHolder(LayoutInflater.from(parent.getContext()).inflate(getLayout(),parent,false));
    }

    protected abstract int getLayout();

    protected abstract VH getViewHolder(View view);

    protected abstract void bindView(VH holder, int position, T t);

    @Override
    public int getItemCount() {
        return mData.size();
    }
}
